<?php
//======================================================================================
//
// Function: Save customer data
//
// Programmer: JKJ
// Date : 2025-05-02
//
// Copyright Reeft A/S (c) - 2025
//======================================================================================
//======================================================================================
// General config
//======================================================================================
include "config/config.php";
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["mode"])) $mode = $_REQUEST["mode"];
else $mode = '';
if (isset($_REQUEST["data"])) {
$data = $_REQUEST["data"];
} else {
$data = 'hovsa';
}
$mode = '*update';
if ( $mode == '' ) {
echo "There's no action....1";
exit;
}
if ( $data == 'hovsa' ) {
echo "There's no data....";
exit;
}
//======================================================================================
// Create reply
//======================================================================================
header('Content-Type: application/json;charset=utf-8');
//======================================================================================
// Connect to some DB
//======================================================================================
include "include/db_connect.php";
//======================================================================================
// Set detault(s)
//======================================================================================
$entries_found = 0;
$currentDate = date('Y-m-d');
$currentTime = date('H:i:s');
$returnCode = '00';
$returnMsg = 'Alles ist gut';
//======================================================================================
// Convert from JSON
//======================================================================================
$json = json_decode($data);
$break = "\r\n";
// Set Defaults
$cust_active = '1';
foreach($json as $obj){
$key = $obj->name;
$value = $obj->value;
// pk_customer
if ( $key == 'modal-input-pk_customer') {
$pk_customer = $value;
if ( $pk_customer == '' ) $pk_customer = 0;
}
// cust_active
if ( $key == 'modal-input-cust_active') {
$cust_active = $value;
if ( $cust_active == '' ) $cust_active = '';
}
// cust_number
if ( $key == 'modal-input-cust_number') {
$cust_number = $value;
if ( $cust_number == '' ) $cust_number = '';
}
// cust_name
if ( $key == 'modal-input-cust_name') {
$cust_name = $value;
if ( $cust_name == '' ) $cust_name = '';
}
// cust_group
if ( $key == 'modal-input-cust_group') {
$cust_group = $value;
if ( $cust_group == '' ) $cust_group = '';
}
// product
if ( $key == 'modal-input-product') {
$product = $value;
if ( $product == '' ) $product = '';
}
// description
if ( $key == 'modal-input-description') {
$description = $value;
if ( $description == '' ) $description = '';
}
// integration_version
if ( $key == 'modal-input-integration_version') {
$integration_version = $value;
if ( $integration_version == '' ) $integration_version = '';
}
// fileformat_ext_file
if ( $key == 'modal-input-fileformat_ext_file') {
$fileformat_ext_file = $value;
if ( $fileformat_ext_file == '' ) $fileformat_ext_file = '';
}
// fileformat_exportterminationfile
if ( $key == 'modal-input-fileformat_exportterminationfile') {
$fileformat_exportterminationfile = $value;
if ( $fileformat_exportterminationfile == '' ) $fileformat_exportterminationfile = '';
}
// fileformat_numberdecimalseparator
if ( $key == 'modal-input-fileformat_numberdecimalseparator') {
$fileformat_numberdecimalseparator = $value;
if ( $fileformat_numberdecimalseparator == '' ) $fileformat_numberdecimalseparator = '';
}
// fileformat_separator
if ( $key == 'modal-input-fileformat_separator') {
$fileformat_separator = $value;
if ( $fileformat_separator == '' ) $fileformat_separator = '';
}
// fileformat_codepage
if ( $key == 'modal-input-fileformat_codepage') {
$fileformat_codepage = $value;
if ( $fileformat_codepage == '' ) $fileformat_codepage = '';
}
// program_dir
if ( $key == 'modal-input-program_dir') {
$program_dir = $value;
if ( $program_dir == '' ) $program_dir = '';
}
// input_dir
if ( $key == 'modal-input-input_dir') {
$input_dir = $value;
if ( $input_dir == '' ) $input_dir = '';
}
// output_dir
if ( $key == 'modal-input-output_dir') {
$output_dir = $value;
if ( $output_dir == '' ) $output_dir = '';
}
// error_dir
if ( $key == 'modal-input-error_dir') {
$error_dir = $value;
if ( $error_dir == '' ) $error_dir = '';
}
// done_dir
if ( $key == 'modal-input-done_dir') {
$done_dir = $value;
if ( $done_dir == '' ) $done_dir = '';
}
// log_dir
if ( $key == 'modal-input-log_dir') {
$log_dir = $value;
if ( $log_dir == '' ) $log_dir = '';
}
// import_functions
if ( $key == 'modal-input-import_functions') {
$import_functions = $value;
if ( $import_functions == '' ) $import_functions = '';
}
// export_functions
if ( $key == 'modal-input-export_functions') {
$export_functions = $value;
if ( $export_functions == '' ) $export_functions = '';
}
// client_id
if ( $key == 'modal-input-client_id') {
$client_id = $value;
if ( $client_id == '' ) $client_id = '';
}
// client_secret
if ( $key == 'modal-input-client_secret') {
$client_secret = $value;
if ( $client_secret == '' ) $client_secret = '';
}
// organizationId
if ( $key == 'modal-input-organizationId') {
$organizationId = $value;
if ( $organizationId == '' ) $organizationId = '';
}
// create_user
if ( $key == 'modal-input-create_user') {
$create_user = $value;
if ( $create_user == '' ) $create_user = '';
}
// create_timestamp
if ( $key == 'modal-input-create_timestamp') {
$create_timestamp = $value;
if ( $create_timestamp == '' ) $create_timestamp = '';
}
// update_user
if ( $key == 'modal-input-update_user') {
$update_user = $value;
if ( $update_user == '' ) $update_user = '';
}
// update_timestamp
if ( $key == 'modal-input-update_timestamp') {
$update_timestamp = $value;
if ( $update_timestamp == '' ) $update_timestamp = '';
}
// company_logo_url
if ( $key == 'modal-input-company_logo_url') {
$company_logo_url = $value;
if ( $company_logo_url == '' ) $company_logo_url = '';
}
// number_of_exports
if ( $key == 'modal-input-number_of_exports') {
$number_of_exports = $value;
if ( $number_of_exports == '' ) $number_of_exports = '';
}
// number_of_imports
if ( $key == 'modal-input-number_of_imports') {
$number_of_imports = $value;
if ( $number_of_imports == '' ) $number_of_imports = '';
}
// ERPname
if ( $key == 'modal-input-ERPname') {
$ERPname = $value;
if ( $ERPname == '' ) $ERPname = '';
}
// ERPdescription
if ( $key == 'modal-input-ERPdescription') {
$ERPdescription = $value;
if ( $ERPdescription == '' ) $ERPdescription = '';
}
// ERPurl
if ( $key == 'modal-input-ERPurl') {
$ERPurl = $value;
if ( $ERPurl == '' ) $ERPurl = '';
}
// json_default_path
if ( $key == 'modal-input-json_default_path') {
$json_default_path = $value;
if ( $json_default_path == '' ) $json_default_path = '';
}
// json_gps_default_path
if ( $key == 'modal-input-json_gps_default_path') {
$json_gps_default_path = $value;
if ( $json_gps_default_path == '' ) $json_gps_default_path = '';
}
// name
if ( $key == 'modal-input-name') {
$name = $value;
if ( $name == '' ) $name = '';
}
// group
if ( $key == 'modal-input-group') {
$group = $value;
if ( $group == '' ) $group = '';
}
}
//======================================================================================
// Get session variables
//======================================================================================
include "include/getsession.php";
//======================================================================================
// Set language
//======================================================================================
include "include/set_language.php";
//======================================================================================
// Adjust if you must
//======================================================================================
//======================================================================================
// Set active, token and other default values
//======================================================================================
$order_create_timestamp = date("Y-m-d H:i:s");
$order_create_timestamp = gmdate('Y-m-d H:i:s', strtotime($order_create_timestamp));
//======================================================================================
// Create SQL
//======================================================================================
$data = [
'pk_customer' => $pk_customer,
'cust_active' => $cust_active,
'cust_number' => $cust_number,
'cust_name' => $cust_name,
'cust_group' => $cust_group,
'product' => $product,
'description' => $description,
'integration_version' => $integration_version,
'fileformat_ext_file' => $fileformat_ext_file,
'fileformat_exportterminationfile' => $fileformat_exportterminationfile,
'fileformat_numberdecimalseparator' => $fileformat_numberdecimalseparator,
'fileformat_separator' => $fileformat_separator,
'fileformat_codepage' => $fileformat_codepage,
'program_dir' => $program_dir,
'input_dir' => $input_dir,
'output_dir' => $output_dir,
'error_dir' => $error_dir,
'done_dir' => $done_dir,
'log_dir' => $log_dir,
'import_functions' => $import_functions,
'export_functions' => $export_functions,
'client_id' => $client_id,
'client_secret' => $client_secret,
'organizationId' => $organizationId,
'create_user' => $create_user,
'create_timestamp' => $create_timestamp,
'update_user' => $update_user,
'update_timestamp' => $update_timestamp,
'company_logo_url' => $company_logo_url,
'number_of_exports' => $number_of_exports,
'number_of_imports' => $number_of_imports,
'ERPname' => $ERPname,
'ERPdescription' => $ERPdescription,
'ERPurl' => $ERPurl,
'json_default_path' => $json_default_path,
'json_gps_default_path' => $json_gps_default_path,
'name' => $name,
'group' => $group
];
$columns = implode(", ", array_keys($data));
$placeholders = ":" . implode(", :", array_keys($data));
if ( $mode == '*add') {
$sql = "INSERT INTO reeft_customer ($columns) VALUES ($placeholders)";
$stmt = $file_db->prepare($sql);
// Bind and execute
$bindings = [];
foreach ($data as $key => $value) {
$bindings[":$key"] = $value;
}
$stmt->execute($bindings);
$last_id = 0;
}
if ( $mode == '*update') {
// Create key = :key pairs, but skip primary key in SET clause
$update_pairs = [];
foreach ($data as $key => $value) {
if ($key != 'pk_customer') {
$update_pairs[] = "$key = :$key";
}
}
$set_clause = implode(", ", $update_pairs);
$sql = "UPDATE reeft_customer SET $set_clause WHERE pk_customer = :pk_customer";
echo $sql;
// $stmt = $file_db->prepare($sql);
// // Reuse same $bindings as above
// $stmt->execute($bindings);
// // Optionally get the last inserted ID
// $last_id = $file_db->lastInsertId();
}
try {
$stmt = $db->prepare($sql);
$stmt->execute($params);
if ($fetch) {
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
return [
'success' => true,
'rowCount' => $stmt->rowCount(),
'lastInsertId' => $db->lastInsertId()
];
} catch (PDOException $e) {
// Log error to file (recommended in production)
error_log("PDO ERROR: " . $e->getMessage() . "\nSQL: $sql\nPARAMS: " . print_r($params, true), 3, 'pdo_errors.log');
// Output to screen for development (disable in production)
echo "<pre style='color: red;'>PDO ERROR: " . htmlspecialchars($e->getMessage()) . "\nSQL: $sql\nPARAMS: ";
print_r($params);
echo "</pre>";
return false;
}
if ( $mode == '*delete') {
$pk_customer = $pk_customer; // Replace this with actual value or passed param
$sql = "DELETE FROM reeft_customer WHERE pk_customer = :pk_customer";
$stmt = $file_db->prepare($sql);
$stmt->execute([':pk_customer' => $pk_customer]);
$rows_deleted = $stmt->rowCount(); // Optional: check if anything was deleted
}
exit;
//======================================================================================
// ADD entry
//======================================================================================
if ( $mode == '*add')
{
$entries_found = 1;
$sql = "INSERT INTO reeft_customer (
pk_customer,
cust_active,
cust_number,
cust_name,
cust_group,
product,
description,
integration_version,
fileformat_ext_file,
fileformat_exportterminationfile,
fileformat_numberdecimalseparator,
fileformat_separator,
fileformat_codepage,
program_dir,
input_dir,
output_dir,
error_dir,
done_dir,
log_dir,
import_functions,
export_functions,
client_id,
client_secret,
organizationId,
create_user,
create_timestamp,
update_user,
update_timestamp,
company_logo_url,
number_of_exports,
number_of_imports,
ERPname,
ERPdescription,
ERPurl,
json_default_path,
json_gps_default_path,
name,
`group`
) VALUES (
:pk_customer,
:cust_active,
:cust_number,
:cust_name,
:cust_group,
:product,
:description,
:integration_version,
:fileformat_ext_file,
:fileformat_exportterminationfile,
:fileformat_numberdecimalseparator,
:fileformat_separator,
:fileformat_codepage,
:program_dir,
:input_dir,
:output_dir,
:error_dir,
:done_dir,
:log_dir,
:import_functions,
:export_functions,
:client_id,
:client_secret,
:organizationId,
:create_user,
:create_timestamp,
:update_user,
:update_timestamp,
:company_logo_url,
:number_of_exports,
:number_of_imports,
:ERPname,
:ERPdescription,
:ERPurl,
:json_default_path,
:json_gps_default_path,
:name,
:group
)";
$data = [
'cust_active' => $cust_active,
'cust_number' => $cust_number,
'cust_name' => $cust_name,
'cust_group' => $cust_group,
'product' => $product,
'description' => $description,
'integration_version' => $integration_version,
'fileformat_ext_file' => $fileformat_ext_file,
'fileformat_exportterminationfile' => $fileformat_exportterminationfile,
'fileformat_numberdecimalseparator' => $fileformat_numberdecimalseparator,
'fileformat_separator' => $fileformat_separator,
'fileformat_codepage' => $fileformat_codepage,
'program_dir' => $program_dir,
'input_dir' => $input_dir,
'output_dir' => $output_dir,
'error_dir' => $error_dir,
'done_dir' => $done_dir,
'log_dir' => $log_dir,
'import_functions' => $import_functions,
'export_functions' => $export_functions,
'client_id' => $client_id,
'client_secret' => $client_secret,
'organizationId' => $organizationId,
'create_user' => $create_user,
'create_timestamp' => $create_timestamp,
'update_user' => $update_user,
'update_timestamp' => $update_timestamp,
'company_logo_url' => $company_logo_url,
'number_of_exports' => $number_of_exports,
'number_of_imports' => $number_of_imports,
'ERPname' => $ERPname,
'ERPdescription' => $ERPdescription,
'ERPurl' => $ERPurl,
'json_default_path' => $json_default_path,
'json_gps_default_path' => $json_gps_default_path,
'name' => $name,
'group' => $group,
'pk_customer' => $pk_customer
];
}
//======================================================================================
// UPDATE entry
//======================================================================================
if ( $mode == '*update')
{
$entries_found = 1;
$sql = "UPDATE reeft_customer SET
cust_active = :cust_active,
cust_number = :cust_number,
cust_name = :cust_name,
cust_group = :cust_group,
product = :product,
description = :description,
integration_version = :integration_version,
fileformat_ext_file = :fileformat_ext_file,
fileformat_exportterminationfile = :fileformat_exportterminationfile,
fileformat_numberdecimalseparator = :fileformat_numberdecimalseparator,
fileformat_separator = :fileformat_separator,
fileformat_codepage = :fileformat_codepage,
program_dir = :program_dir,
input_dir = :input_dir,
output_dir = :output_dir,
error_dir = :error_dir,
done_dir = :done_dir,
log_dir = :log_dir,
import_functions = :import_functions,
export_functions = :export_functions,
client_id = :client_id,
client_secret = :client_secret,
organizationId = :organizationId,
create_user = :create_user,
create_timestamp = :create_timestamp,
update_user = :update_user,
update_timestamp = :update_timestamp,
company_logo_url = :company_logo_url,
number_of_exports = :number_of_exports,
number_of_imports = :number_of_imports,
ERPname = :ERPname,
ERPdescription = :ERPdescription,
ERPurl = :ERPurl,
json_default_path = :json_default_path,
json_gps_default_path = :json_gps_default_path,
name = :name,
`group` = :group
WHERE pk_customer = :pk_customer";
$stmt = $file_db->prepare($sql);
$stmt->execute([
':cust_active' => $cust_active,
':cust_number' => $cust_number,
':cust_name' => $cust_name,
':cust_group' => $cust_group,
':product' => $product,
':description' => $description,
':integration_version' => $integration_version,
':fileformat_ext_file' => $fileformat_ext_file,
':fileformat_exportterminationfile' => $fileformat_exportterminationfile,
':fileformat_numberdecimalseparator' => $fileformat_numberdecimalseparator,
':fileformat_separator' => $fileformat_separator,
':fileformat_codepage' => $fileformat_codepage,
':program_dir' => $program_dir,
':input_dir' => $input_dir,
':output_dir' => $output_dir,
':error_dir' => $error_dir,
':done_dir' => $done_dir,
':log_dir' => $log_dir,
':import_functions' => $import_functions,
':export_functions' => $export_functions,
':client_id' => $client_id,
':client_secret' => $client_secret,
':organizationId' => $organizationId,
':create_user' => $create_user,
':create_timestamp' => $create_timestamp,
':update_user' => $update_user,
':update_timestamp' => $update_timestamp,
':company_logo_url' => $company_logo_url,
':number_of_exports' => $number_of_exports,
':number_of_imports' => $number_of_imports,
':ERPname' => $ERPname,
':ERPdescription' => $ERPdescription,
':ERPurl' => $ERPurl,
':json_default_path' => $json_default_path,
':json_gps_default_path' => $json_gps_default_path,
':name' => $name,
':group' => $group,
':pk_customer' => $pk_customer
]);
}
//echo "$sql";
include "include/db_run_sql_safe.php";
if ( $entries_found == 0 ) {
$returnCode = '99';
$returnMsg = 'No data found';
} else {
$returnCode = '00';
$returnMsg = 'Alles ist gut';
}
//======================================================================================
// Create header
//======================================================================================
// Create header
$aryHeader = array();
$aryHeader["mode"] = $mode;
$aryHeader["returnCode"] = $returnCode;
$aryHeader["returnMsg"] = $returnMsg;
$aryHeader["entries_found"] = $entries_found;
$aryHeader["currentDate"] = $currentDate;
$aryHeader["currentTime"] = $currentTime;
// Create array and prepare for json encoding
$returnJson["header"] = $aryHeader;
//======================================================================================
// Paint it black
//======================================================================================
echo(json_encode($returnJson));
?>